package com.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class UserLoginActivity extends Activity {

	//用户名,年龄,密码文本框
	private EditText name, age, pass;
	//注册按钮
	private Button regButton;
	//性别单选框
	private RadioGroup sexRadioGroup;
	//篮球,足球,乒乓,网球复选框
	private CheckBox basketball, football, pingpang, tennis;
	//城市下拉框
	private Spinner cityItems;
	//城市数组
	private String[] cities = null;
	//爱好复选框集合
	private List<CheckBox> hobbies = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        //获取城市下拉框对象
        cityItems = (Spinner)findViewById(R.id.cityItem);
        //获取城市数组
        cities = getResources().getStringArray(R.array.cities);
        //设置城市下拉框值
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(UserLoginActivity.this, 
        		android.R.layout.simple_spinner_item, cities);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cityItems.setAdapter(adapter);
        
        //获取用户名文本框
        name = (EditText)findViewById(R.id.userNameValue);
        //获取年龄文本框
        age = (EditText)findViewById(R.id.ageValue);
        //获取密码文本框
        pass = (EditText)findViewById(R.id.passwordValue);
        
        //获取性别单选按钮组
        sexRadioGroup = (RadioGroup)findViewById(R.id.sexMenu);
        
        hobbies = new ArrayList<CheckBox>(4);
        //获取篮球复选框
        basketball = (CheckBox)findViewById(R.id.checkboxBaskeball);
        //获取足球复选框
        football = (CheckBox)findViewById(R.id.checkboxFootball);
        //获取乒乓复选框
        pingpang = (CheckBox)findViewById(R.id.checkboxPingPang);
        //获取网球复选框
        tennis = (CheckBox)findViewById(R.id.checkboxTennis);
        
        hobbies.add(basketball);
        hobbies.add(football);
        hobbies.add(pingpang);
        hobbies.add(tennis);
        
        //获取注册按钮
        regButton = (Button)findViewById(R.id.registerButton);
        regButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean flag = validataUser();
				if (flag) {
					StringBuilder strBuf = new StringBuilder(50);
					strBuf.append(getResources().getString(R.string.you_info)).append("\n");
					strBuf.append(getResources().getString(R.string.nameString));
					strBuf.append(name.getText().toString().trim()).append("\n");
					strBuf.append(getResources().getString(R.string.ageString));
					strBuf.append(age.getText().toString().trim()).append("\n");
					strBuf.append(getResources().getString(R.string.sexString));
					strBuf.append(getSex()).append("\n");
					strBuf.append(getResources().getString(R.string.hobbyString));
					strBuf.append(getHobby()).append("\n");
					strBuf.append(getResources().getString(R.string.cityString));
					strBuf.append(getCity()).append("\n");
					
					new AlertDialog.Builder(UserLoginActivity.this)
					.setTitle(R.string.alert_title)
					.setCancelable(false)
					.setMessage(strBuf.toString())
					.setPositiveButton(getResources().getString(R.string.ok), 
							new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							ProgressDialog.show(UserLoginActivity.this, 
									getResources().getString(R.string.registering),
									getResources().getString(R.string.wait))
									.setCancelable(true);
						}
					})
					.setNegativeButton(getResources().getString(R.string.modify), 
							new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					})
					.show();
				}
			}
		});
    }
    
	/**
	 * 验证用户基本信息
	 * 
	 * @return true:通过<br>
	 *         false:不通过
	 */
    private boolean validataUser(){
    	if ("".equals(name.getText().toString().trim())) {
    		name.setError("用户名不能为空!");
    		return false;
    	}
    	if ("".equals(pass.getText().toString())) {
    		pass.setError("密码不能为空!");
    		return false;
    	}
    	if ("".equals(age.getText().toString().trim())) {
    		age.setError("年龄不能为空!");
    		return false;
    	}
    	return true;
    }
    
    /**
     * 获取性别
     * @return String
     */
    private String getSex() {
    	RadioButton radioButton = (RadioButton)findViewById(sexRadioGroup.getCheckedRadioButtonId());
    	return radioButton.getText().toString();
    }
    
    /**
     * 获取爱好
     * @return String
     */
    private String getHobby() {
    	StringBuilder sb = new StringBuilder(15);
    	for (CheckBox cb : hobbies) {
    		if (cb.isChecked()) {
    			sb.append(cb.getText().toString());
    			sb.append(",");
    		}
    	}
    	if (!"".equals(sb.toString())) {
    		return sb.toString().substring(0, sb.toString().length() - 1);
    	} else {
    		return getResources().getString(R.string.not_hobby);
    	}
    }
    
    /**
     * 获取城市
     * @return String
     */
    private String getCity() {
    	return cities[(int)cityItems.getSelectedItemId()];
    }

    /**
     * 创建菜单选项
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_user_login, menu);
        return true;
    }
}
